<?php

global $CFG;
require_once("{$CFG->libdir}/formslib.php");



/**
 * Abstract form that is used in the guestbook
 *
 * @author Kirill Krasnoschekov
 */
abstract class guestbook_form extends moodleform {
    
    /**
     * Get rendered form 
     * 
     * @return string Rendered form
     * @author Kirill Krasnoschekov
     */
    function to_html() {
        return $this->_form->toHtml();
    }
    
    /**
     * Update submitted data of the form
     * 
     * @param array $submission Hash array of submitted form values
     * @param array $files
     * @author Kirill Krasnoschekov
     */
    function update_submission($submission, $files) {
        return $this->_form->updateSubmission($submission, $files);
    }
    
    /**
     * Reset submitted values to empty
     * 
     * @author Kirill Krasnoschekov
     */
    function reset_submission() {
        return $this->update_submission(null, null);
    }
}



/**
 * Form for messages
 * 
 * @author Kirill Krasnoschekov
 */
class guestbook_message_form extends guestbook_form {

    /**
     * Elements definition
     * 
     * @author Kirill Krasnoschekov
     */
    function definition() {
        $mform =& $this->_form;
        
        // Elements
        $mform->addElement('text', 'name', get_string('name', 'block_guestbook'));
        $mform->addElement('textarea', 'text', get_string('text', 'block_guestbook'),
                array('cols' => 40, 'rows' => 10));

        // Rules
        $mform->addRule('name', get_string('field_required', 'block_guestbook'), 'required');
        $mform->addRule('text', get_string('field_required', 'block_guestbook'), 'required');

        // Filters
        $mform->applyFilter('name', 'trim');
        $mform->applyFilter('text', 'trim');
                
        // Buttons
        $mform->addElement('reset', 'reset', get_string('reset', 'block_guestbook'));
        $mform->addElement('submit', 'submit', get_string('submit', 'block_guestbook'));
    }
}



/**
 * Form for message updating
 */
class guestbook_message_update_form extends guestbook_message_form {
    
    /**
     * Elements definition
     * 
     * @author Kirill Krasnoschekov
     */
    function definition() {
        $mform =& $this->_form;
        
        // Header
        $mform->addElement('header','message_update_form', 
                get_string('message_update_form', 'block_guestbook'));

        // Hidden: mode and message id
        $mform->addElement('hidden', 'mode', 'update');
        $mform->addElement('hidden', 'msgid');
        
        parent::definition();
    }
}



/**
 * Form for adding message
 * 
 * @author Kirill Krasnoschekov
 */
class guestbook_message_add_form extends guestbook_message_form {
    
    /**
     * Elements definition
     * 
     * @author Kirill Krasnoschekov
     */
    function definition() {
        $mform =& $this->_form;
        
        // Header
        $mform->addElement('header','message_add_form', 
                get_string('message_add_form', 'block_guestbook'));
                
        // Message id
        $mform->addElement('hidden', 'mode', 'add');

        parent::definition();
    }
    
}



/**
 * Form for comments
 * 
 * @author Kirill Krasnoschekov
 */
class guestbook_comment_form extends guestbook_form {

    /**
     * Elements definition
     * 
     * @author Kirill Krasnoschekov
     */
    function definition() {
        $mform =& $this->_form;
        
        // Header
        $mform->addElement('header','comment_form', 
                get_string('comment_add_form', 'block_guestbook'));

        // Mode and message id 
        $mform->addElement('hidden', 'mode', 'add');
        $mform->addElement('hidden', 'msgid');
        
        // Fields
        $mform->addElement('text', 'name', get_string('name', 'block_guestbook'));
        $mform->addElement('textarea', 'text', get_string('text', 'block_guestbook'),
                array('cols' => 40, 'rows' => 10));
                
        // Buttons
        $mform->addElement('reset', 'reset', get_string('reset', 'block_guestbook'));
        $mform->addElement('submit', 'submit', get_string('submit', 'block_guestbook'));
        

        // Rules
        $mform->addRule('name', get_string('field_required', 'block_guestbook'), 'required');
        $mform->addRule('text', get_string('field_required', 'block_guestbook'), 'required');

        // Filters
        $mform->applyFilter('name', 'trim');
        $mform->applyFilter('text', 'trim');        
    }
}